﻿
using Repositories.Interfaces;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System;
namespace Repositories.Implementations
{
    public class UserLoginRepository : BaseRepository<UserLogin>, IUserLoginRepository
    {
        public UserLoginRepository()
            : base()
        {
        }
        /// <summary>
        /// Gets the name of the entity set.
        /// </summary>
        /// <value>The name of the entity set.</value>
        protected override string EntitySetName
        {
            get
            {
                return "ASE_DatabaseEntities.UserLogin";
            }
        }

        public void Refresh()
        {
            ASEDbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, ASEDbContext.UserLogin);
        }

        private IUserLoginRepository _UserLoginRepository;

        /// <summary>
        /// Gets the UserLogin repository.
        /// </summary>
        /// <value>The UserLogin repository.</value>
        public IUserLoginRepository UserLoginRep
        {
            get
            {
                if (_UserLoginRepository == null)
                {
                    _UserLoginRepository = DIHelper.Resolve<IUserLoginRepository>();
                }
                return _UserLoginRepository;
            }
        }





        /// <summary>
        /// Finds the by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>The UserLogin with input ID.</returns>
        public UserLogin FindById(int id)
        {
            IQueryable<UserLogin> returnValue = UserLoginRep.GetQuery(itm => itm.Id == id);
            if (returnValue.Count() == 0)
                return null;
            return returnValue.FirstOrDefault();
        }

      
        /// <summary>
        /// Edits the UserLogin.
        /// </summary>
        /// <param name="item">The item.</param>
        public void EditUserLogin(UserLogin item)
        {
            UserLoginRep.Update(item);
        }

        public bool FindByUserName(string userName,string password)
        {
            IQueryable<UserLogin> returnValue = UserLoginRep.GetQuery(itm => itm.Username == userName && itm.Password==password);
            if (returnValue.Any())
                return true;
            return false;
        }


        /// <summary>
        /// Deletes the UserLogin.
        /// </summary>
        /// <param name="item">The item.</param>
        public void DeleteUserLogin(UserLogin item)
        {
            UserLoginRep.Delete(item);
        }


        /// <summary>
        /// Applies the changes to database.
        /// </summary>
        public void ApplyTheChanges()
        {
            UserLoginRep.UnitOfWork.SaveChanges();
        }

        /// <summary>
        /// Accepts all changes.
        /// </summary>
        public void AcceptAllChanges()
        {
            UserLoginRep.UnitOfWork.AcceptAllChanges();
        }


    }
}
